
<template>
  <el-form
    ref="departmentForm"
    :model="form"
    :size="size"
    :rules="rules"
    :label-suffix="labelSuffix"
    :label-position="labelPosition"
    :label-width="labelWidth"
    @submit.native.prevent
  >
    <el-form-item
      ref="formInput.name"
      label="单位名称"
      prop="name"
      class="form-item--medium"
    >
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item
      ref="formInput.mark"
      label="描述"
      prop="mark"
      class="form-item--medium"
    >
      <el-input
        v-model="form.mark"
        type="textarea"
      />
    </el-form-item>
    <number-input
      v-model="form.listorder"
      label="前端显示顺序"
      prop="listorder"
      :step="1"
      :min="0"
      step-strictly
    >
      <div
        slot="hint"
        class="form-item__tips"
      >
        设置的值越小越靠前
      </div>
    </number-input>
    <el-form-item
      label="是否启用"
      prop="status"
    >
      <el-switch
        v-model="form.status"
        :active-value="10"
        :inactive-value="0"
      />
    </el-form-item>
    <slot name="action">
      <form-action v-if="actionVisible">
        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit"
          >{{ submitButtonText }}</el-button>
        </el-form-item>
      </form-action>
    </slot>
  </el-form>
</template>

<script>
import { objectFillMerge } from '@/utils'
import NumberInput from '@/components/Form/NumberInput'
import FormAction from '@/components/Form/FormAction'
export default {
  name: 'DepartmentForm',
  components: {
    NumberInput,
    FormAction
  },
  props: {
    primaryKey: {
      type: [String, Number],
      default: 0
    },
    labelPosition: {
      type: String,
      default: ''
    },
    labelWidth: {
      type: String,
      default: '20%'
    },
    labelSuffix: {
      type: String,
      default: '：'
    },
    size: {
      type: String,
      default: 'small'
    },
    submitButtonText: {
      type: String,
      default: '提交保存'
    },
    actionVisible: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        mark: '',
        listorder: 0,
        status: 0
      },
      rules: {
        name: [{ required: true, message: '请输入责任单位名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.onLoad()
  },
  mounted() {
    console.log('on mounted form')
  },
  methods: {
    onLoad() {
      this.$http.setting.updateDepartment(this.form, this.primaryKey, false).then(res => {
        this.form = objectFillMerge(this.form, res.data)
      })
    },
    onSubmit() {
      this.$refs.departmentForm.validate((valid, obj) => {
        if (valid) {
          console.log(this.form)
          this.$http.setting.updateDepartment(this.form, this.primaryKey).then(res => {
            this.$emit('submit', res.data)
          })
        } else {
          let firstErrorElname = ''
          for (const i in obj) {
            firstErrorElname = `formInput.${i}`
            break
          }
          var dom = this.$refs[firstErrorElname]
          console.log(dom, this.$refs)
          if (dom) {
            dom.$refs.input && dom.$refs.input.focus()
            dom.$el.scrollIntoView({
              // 滚动到指定节点
              block: 'center', // 值有start,center,end，nearest，当前显示在视图区域中间
              behavior: 'smooth' // 值有auto、instant,smooth，缓动动画（当前是慢速的）
            })
          }
          console.log('error submit!!', this.form)
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
.brand-upload {
  &__thumb,
  &__cover {
    .upload-square {
      background: #f2f3f7;
    }
  }
}
</style>
